﻿using System;
using UnityEngine;

public class ObjectAsset<T> : IObjectAsset, IReference where T : UnityEngine.Object
{
    private T _target;

    private Action<bool, T> assetCb;

    public static ObjectAsset<T> Create(Action<bool, T> callBack)
    {
        var objectAsset = ReferencePool.Acquire<ObjectAsset<T>>();
        objectAsset.assetCb = callBack;
        return objectAsset;
    }

    public void Clear()
    {
        _target = null;
    }

    public void Invoke(bool suc)
    {
        assetCb?.Invoke(suc, _target);
    }

    public void Invoke(bool suc,T t)
    {
        assetCb?.Invoke(suc, t);
    }

    public void Release()
    {
        ReferencePool.Recycle(this);
    }

    public void SetTarget(object result)
    {
        _target = (T)result;
    }
}

public interface IObjectAsset
{
    public void SetTarget(object result);

    public void Invoke(bool suc);

    public void Release();
}